"use strict";

import Vue from 'vue';
import axios from "axios";
import store from '../store'
// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  baseURL:"http://47.115.51.185",
  // baseURL:"http://musicapi.leanapp.cn",
  // baseURL:"https://musci.kele8.cn",
  // baseURL:"http://localhost:3000",
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);
//前置拦截器 在ajax请求之前执行.
_axios.interceptors.request.use(
  function(config) {
    // console.log('前置配置拦截器..',config);
    //在请求之前判断当前应用里面是否有token 如果有就带上token
    if(store.getters.token){
      config.headers={
        'Authori-zation':'Bearer '+store.getters.token
      }
    }
    // console.log('配置之后 ajax配置',config);
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
//后置拦截
_axios.interceptors.response.use(
  function(response) {
    // Do something with response data
    return response;
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function(Vue) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;
